package org.aspectsense.middleware.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import javax.swing.JPanel;

import org.aspectsense.middleware.Repository;




public class DrawGraph extends JPanel {

	
	private Hashtable <String, Point> serviceNodesCoordinates;
	private Hashtable <String, Point> componentNodesCoordinates;
	
	DrawGraph()
	{
		super();
		serviceNodesCoordinates = new Hashtable <String, Point>();
		componentNodesCoordinates = new Hashtable <String, Point>();
	}
	
	
	
	public void paintComponent(Graphics g) {
        super.paintComponent(g);   
        
        int space=20, X = 5, Y = 10, ar_width=20, ar_height=20, ar_angle=60, startAngle=0;
        double lengthX=0.0, lengthY=0.0, lineLength=0.0;   
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /*
        
        
        LinkedList <BigraphComponentNode> componentsSet = AdaptationGraph.getComponentsSet();
        LinkedList <BigraphServiceNode> servicesSet = AdaptationGraph.getServicesSet();
        int componentsNo = 0, servicesNo = 0;
        BigraphComponentNode cn;
        BigraphServiceNode sn;
        int d, d1, d2;
        Point endPoint, startPoint;
        Dimension size = getSize();
        int panelWidth = size.width;
    	Iterator <BigraphServiceNode>itr;
    	Iterator <BigraphComponentNode>itr2;
        if (componentsSet!=null)
        	componentsNo = componentsSet.size();
        if (servicesSet != null)
        	servicesNo = servicesSet.size();
        if (componentsNo>0)
        	d1 = (panelWidth/componentsNo)-space;
        else
        	d1 = 0;
        if (servicesNo!=0)
        	d2 = (panelWidth/servicesNo)-space;
        else
        	d2 = 0;
        
        d = Math.min(d1,d2);
        
       
        //draw service nodes
        for (int i=0; i<servicesNo; i++)
        {
        	sn = servicesSet.get(i);
        	if (Repository.isEndUserService(sn.getServiceName()))
        		g.setColor(Color.red);
        	else
        		g.setColor(Color.green);
      	    g.fillOval(X, Y, 10, 10);
            g.drawOval(X, Y, 10, 10);
            g.setColor(Color.black);
            g.drawString(sn.getShortServiceName(), X+8, Y);
            serviceNodesCoordinates.put(sn.getShortServiceName(), new Point (X,Y));
            X += space+d;

        }
        
        X = 5;
        Y = ((int) size.getHeight()) - 20;
        //draw component nodes
        for (int i=0; i<componentsNo; i++)
        {
        	cn = componentsSet.get(i);
        	 g.fillOval(X, Y, 10, 10);
             g.setColor(Color.black);
             g.drawOval(X, Y, 10, 10);
             g.drawString(cn.getShortComponentName(), X+8, Y);
             componentNodesCoordinates.put(cn.getShortComponentName(), new Point (X,Y));
             X += space+d;
 
        }
        
        Graphics2D g2 = (Graphics2D) g;

        //Draw lines
        for (int i=0; i<componentsNo; i++)
        {
        	cn = componentsSet.get(i);
        	endPoint = componentNodesCoordinates.get(cn.getShortComponentName());
        	
        	HashSet<BigraphServiceNode> requiredServices = cn.getRequiredServices();
        	itr = requiredServices.iterator();
        	while (itr.hasNext())
        	{
        		sn = itr.next();
        		startPoint = serviceNodesCoordinates.get(sn.getShortServiceName());
        		
        		g2.setColor(Color.gray);
        		g2.draw(new Line2D.Double(startPoint.getX(), startPoint.getY()+5.00, endPoint.getX(), endPoint.getY()-5.00));
        		
        		// Calulate the length of the line
        		lengthX = endPoint.getX() - startPoint.getX();
        		lengthY = endPoint.getY() - startPoint.getY();
        		lineLength = Math.sqrt (lengthX*lengthX + lengthY*lengthY);

        		startAngle = (int)Math.toDegrees(Math.asin(lengthY/lineLength));
        		if (lengthX > 0)
        			startAngle = (180 - startAngle - 30);
        		else
        			startAngle = (startAngle - 30);

        		// Draw the arrow head
        		g.fillArc ( ((int) (endPoint.getX() - (ar_width/2))), ((int)(endPoint.getY() - (ar_height/2)+1)) ,ar_width, ar_height,startAngle,ar_angle);
        		
        	}
        	
        
        }
      //Draw lines from components to services
        for (int i=0; i<servicesNo; i++)
        {
        	sn = servicesSet.get(i);
        	endPoint = serviceNodesCoordinates.get(sn.getShortServiceName());
        	
        	HashSet<BigraphComponentNode> providers = sn.getProviders();
        	itr2 = providers.iterator();
        	while (itr2.hasNext())
        	{
        		cn = itr2.next();
        		startPoint = componentNodesCoordinates.get(cn.getShortComponentName());
        		
        		g2.setColor(Color.blue);
        		g2.draw(new Line2D.Double(startPoint.getX(), startPoint.getY()+5.00, endPoint.getX(), endPoint.getY()+5.00));
        		
        		// Calulate the length of the line
        		lengthX = endPoint.getX() - startPoint.getX();
        		lengthY = endPoint.getY() - startPoint.getY();
        		lineLength = Math.sqrt (lengthX*lengthX + lengthY*lengthY);

        		startAngle = (int)Math.toDegrees(Math.asin(lengthY/lineLength));
        		if (lengthX > 0)
        			startAngle = (180 - startAngle - 30);
        		else
        			startAngle = (startAngle - 30);

        		// Draw the arrow head
        		g.fillArc ( ((int) (endPoint.getX() - (ar_width/2))), ((int)(endPoint.getY()+5 - (ar_height/2)+1)) ,ar_width, ar_height,startAngle,ar_angle);
        		
        	}
        	
        
        }*/
      

   }
	
	/**
	 * Mark the service provision paths that maximize utility
	 */
	/*public void drawMaxPaths ()
	{
		LinkedList <HashMap<BigraphServiceNode,BigraphComponentNode>> maxPaths = AdaptationGraph.maxPaths;
		HashMap<BigraphServiceNode,BigraphComponentNode> curr;
		BigraphComponentNode cn;
		BigraphServiceNode sn;
		Point endPoint, startPoint;
		Set <BigraphServiceNode> keys;
		 Graphics2D g2 = (Graphics2D) this.getGraphics();
		if (maxPaths==null || maxPaths.size()<=0)
			return;
		Iterator <HashMap<BigraphServiceNode,BigraphComponentNode>> itr = maxPaths.iterator();
		Iterator <BigraphServiceNode> sitr;
		while (itr.hasNext())
		{
			curr = itr.next();
			keys = curr.keySet();
			sitr = keys.iterator();
			while (sitr.hasNext())
			{
				sn = sitr.next();
				cn = curr.get(sn);
				endPoint = serviceNodesCoordinates.get(sn.getShortServiceName());
				startPoint = componentNodesCoordinates.get(cn.getShortComponentName());
				g2.setColor(Color.red);
	    		g2.draw(new Line2D.Double(startPoint.getX(), startPoint.getY()+5.00, endPoint.getX(), endPoint.getY()+5.00));
			}
			
			
		}
	}*/

	
}